package edu.hnu.productserver.controller;


import edu.hnu.commonutils.Result;
import edu.hnu.commonutils.model.User;
import edu.hnu.productserver.auth.JwtOperator;
import edu.hnu.productserver.service.StocksService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/product")
public class StocksController {

    @Autowired
    StocksService stocksService;


    @Autowired
    JwtOperator jwtOperator;
    private User parseToken(String token){
        Claims claims = jwtOperator.getClaimsFromToken(token);
        User user = new User(-1L,-2,false);
        try {
            Long id = Long.parseLong(claims.get("id").toString());
            int userType= Integer.parseInt(claims.get("user_type").toString());
            if (userType != 0) {
                // 判断是否是不是生产商
                user.setAuthorized(false);
            }else {
                user.setUserType(userType);
                user.setId(id);
                user.setAuthorized(true);
            }
        } catch (NumberFormatException e) {
            user.setAuthorized(false);
            return user;
        }
        return user;
    }
    @GetMapping("/stocksInfo")
    public Result stocksInfo(){
        return stocksService.stocksInfo();
    }
}
